package com.shuzi.MediaShare.gui.love;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.control.ImageLoaderManager;
import com.shuzi.MediaShare.model.MediaType;
import com.shuzi.MediaShare.model.MovieInfo;

/**
 * 电影适配器
 * 
 * @author lung 下午9:29:34 2013
 */
public class LoveHomeAdapter extends BaseAdapter
{
	private Context context;

	private List<MovieInfo> mediaInfos;

	private ImageLoaderManager imageLoaderManager;

	public LoveHomeAdapter(Context context, List<MovieInfo> mediaInfos)
	{
		this.context = context;
		this.mediaInfos = mediaInfos;
		imageLoaderManager = ImageLoaderManager.getInstace();
	}

	@Override
	public int getCount()
	{
		return mediaInfos.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mediaInfos.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	static class ViewHolder
	{
		/** 电影名称 */
		public TextView movieName;

		/** 地区 */
		public TextView movieArea;

		/** 电影年代 */
		public TextView movieReleaseTime;

		/** 电影类型 */
		public TextView movieType;

		/** 电影图片 */
		public ImageView img;

		public ViewHolder(View baseView)
		{
			movieName = (TextView) baseView.findViewById(R.id.movie_name);
			movieArea = (TextView) baseView.findViewById(R.id.movie_area);
			movieReleaseTime = (TextView) baseView
					.findViewById(R.id.movie_release_time);
			movieType = (TextView) baseView.findViewById(R.id.movie_type);
			img = (ImageView) baseView.findViewById(R.id.movie_img);
			setImageParams(img);
		}

	}

	/**
	 * @param img2
	 */
	private static void setImageParams(final ImageView img2)
	{
		
			ViewTreeObserver vto = img2.getViewTreeObserver();
			vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
			{
				@Override
				public void onGlobalLayout()
				{

					img2.getViewTreeObserver().removeGlobalOnLayoutListener(
							this);
					LayoutParams layoutParams = (LayoutParams) img2
							.getLayoutParams();
					layoutParams.height = img2.getWidth() / 2 * 3;
					img2.setLayoutParams(layoutParams);

				}

			});
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;
		if (convertView == null)
		{
			convertView = View.inflate(context, R.layout.movie_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		MovieInfo info = mediaInfos.get(position);

		holder.movieName.setText(info.getName_cn());
		holder.movieArea.setText(info.getProd_area());
		holder.movieReleaseTime.setText(info.getRelease_time());
		MediaType type = Constant.tMap.get(info.getType());
		int genre = Integer.parseInt(info.getGenre());
		// holder.movieType.setText(type.typeList[StringUtils.getTwoCount(genre)]);
		// holder.movieType.setText(""+StringUtils.getTwoCount(genre));
		// Log.e("lung",
		// "genre = "+genre+"-------"+StringUtils.getTwoCount(genre));
		imageLoaderManager.displayImage(info.getPoster_link(), holder.img,
				imageLoaderManager.options);

		return convertView;
	}
}
